package com.example.studentalarm.widget;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.studentalarm.R;
import com.example.studentalarm.utils.StringUtil;


/**
 * 基本对话框 处理按钮点击事件，需设置Handler
 */
public class BaseDialog extends Dialog {
    public final static String TAG = "BaseDialog";
    private Context context;
    private TextView text_message, text_title;
    private Button btn_left, btn_right;
    private View spilte_sign;

    private String tipmessage = null, tip_title = null, lefttext = "取消", righttext = "确定";


    private boolean isCoupon = false;

    public void setCoupon(boolean isCoupon) {
        this.isCoupon = isCoupon;
    }


    public BaseDialog(Context context) {
        super(context, R.style.fragment_offline_dialog);
        this.context = context;
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    public BaseDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    public void setTipMessage(String tip) {
        tipmessage = tip;
    }

    public void setLeftText(String text) {
        this.lefttext = text;
    }

    public void setRightText(String text) {
        this.righttext = text;
    }

    public void setTitle(String str) {
        this.tip_title = str;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_view_basedialog);
        text_message = (TextView) findViewById(R.id.text_message);
        btn_left = (Button) findViewById(R.id.btn_left);
        btn_right = (Button) findViewById(R.id.btn_right);
        spilte_sign = (View) findViewById(R.id.spilt_sign);
        text_title = (TextView) findViewById(R.id.text_title);
    }

    /**
     * 隐藏左右按钮之间的分割线
     */
    public void hideSpilte_sign() {
        if (spilte_sign != null) {
            spilte_sign.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏右边按钮
     */
    public void hideRightBtn() {
        if (btn_right != null) {
            btn_right.setVisibility(View.GONE);
        }
    }

    /**
     * 显示最上面标题
     */
    public void showTitle() {
        if (text_title != null) {
            text_title.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 添加点击事件 需要放在dialog.show();之后
     *
     * @param click
     */
    public void setClickListener(final ButtonClick click) {
        btn_left.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
                click.leftClick();
            }
        });
        btn_right.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
                click.rightClick();
            }
        });
    }

    @Override
    public void show() {
        super.show();
        if (tipmessage != null)
            text_message.setText(tipmessage);
        if (lefttext != null)
            btn_left.setText(lefttext);
        if (righttext != null)
            btn_right.setText(righttext);
        if (!StringUtil.isEmpty(tip_title)) {
            text_title.setText(tip_title);
            text_title.setVisibility(View.VISIBLE);
        }
        if (isCoupon) {
            if (tip_title != null)
                text_title.setText(tip_title);
            text_message.setTextSize(22);
            text_message.setTextColor(context.getResources().getColor(R.color.font_red));
            if (tip_title != null)
                text_title.setVisibility(View.VISIBLE);
            else
                text_title.setVisibility(View.GONE);
        }

    }


    public interface ButtonClick {
        void leftClick();

        void rightClick();
    }


}
